package myList1;

public class MergeArrayList {
    public static void main(String[] args) {
        Integer[] a = {1, 2, 3, 4, 5};
        MyArrayList<Integer> L = new MyArrayList<Integer>();
        L.createList(a);
        System.out.println("L: " + L);
    }

    public static MyArrayList<Integer> merge2(MyArrayList<Integer> listA,
                                              MyArrayList<Integer> listB) {
        MyArrayList<Integer> listC = new MyArrayList<Integer>();
        int i = 0, j = 0;            //i用于遍历A,j用于遍历B
        while (i < listA.size() && j < listB.size())    //两个表均没有遍历完
        {
            if (listA.getElem(i) < listB.getElem(j)) {
                listC.add(listA.getElem(i));        //将较小的A中元素添加到C中
                i++;
            } else {
                listC.add(listB.getElem(j));        //将较小的B中元素添加到C中
                j++;
            }
        }
        while (i < listA.size())            //若A没有遍历完毕
        {
            listC.add(listA.getElem(i));
            i++;
        }
        while (j < listB.size())        //若B没有遍历完毕
        {
            listC.add(listB.getElem(j));
            j++;
        }
        return listC;
    }

}

